#include "MyBuffer.h"

MyBuffer::MyBuffer(void *buf, unsigned long size)
{
    startAddr = (unsigned char*)buf;
    currAddr  = (unsigned char*)buf;
    endAddr   = (unsigned char*)buf + size;
}

void MyBuffer::SetBuffer(void *buf, unsigned long size)
{
    startAddr = (unsigned char*)buf;
    currAddr  = (unsigned char*)buf;
    endAddr   = (unsigned char*)buf + size;
}

unsigned long MyBuffer::Write(void *src, unsigned long numBytes)
{
    unsigned i;
    unsigned long bytesWrote = 0;
    if ((currAddr+numBytes)>endAddr)
    {		
        return 0; // Overflow
    }	
    for (i=0; i<numBytes; ++i)
    {
        ((unsigned char*)currAddr)[i] = ((unsigned char*)src)[i];
        ++bytesWrote;
    }
    currAddr += i;
    return bytesWrote;
}

unsigned long MyBuffer::Write(int src)
{
    return Write(&src, sizeof(src));
}

unsigned long MyBuffer::Write(unsigned char src)
{
	*currAddr = src;
	++currAddr;
	return 1;
}

unsigned long MyBuffer::Write(bool src)
{
    return Write(&src, sizeof(bool));
}

void MyBuffer::Read(void *dst, unsigned long numBytes)
{
    unsigned i;
    if ((currAddr+numBytes)>endAddr)
    {		
        return; // Overflow
    }
    for (i=0; i<numBytes; ++i)
    {
        ((unsigned char*)dst)[i] = ((unsigned char*)currAddr)[i];
    }
    currAddr += i;
}
